﻿using System;
using System.Collections.Generic;
using System.Text;

namespace GuessTicketClass.Utils
{
    /// <summary>
    /// 数字处理类
    /// </summary>
    public class IntUtil
    {
        private IntUtil() { }

        /// <summary>
        /// 转换BYTE 失败返回defaultValue 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static byte ToSafeByte(object value, byte defaultValue)
        {
            byte result = defaultValue;
            if ((null != value) && (!value.ToString().Equals(string.Empty)))
            {
                try
                {
                    result = Convert.ToByte(value);
                }
                catch { }
            }
            return result;
        }
        /// <summary>
        /// 转换BYTE 失败返回0
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte ToSafeByte(object value)
        {
            return ToSafeByte(value, 0);
        }
  
        /// <summary>
        /// 转换整形 失败返回defaultValue
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static int ToSafeInt(object value, int defaultValue)
        {
            int result = defaultValue;
            if ((null != value) && (!value.ToString().Equals(string.Empty)))
            {
                try
                {
                    result = Convert.ToInt32(value);
                }
                catch {
                    result = 0;
                }
            }
            return result;
        }
        /// <summary>
        /// 转换整形 失败返回0
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int ToSafeInt(object value)
        {
            return ToSafeInt(value, 0);
        }
        /// <summary>
        /// 转换long 失败返回defaultValue
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static long ToSafeLong(object value, long defaultValue)
        {
            long  result = defaultValue;
            if ((null != value) && (!value.ToString().Equals(string.Empty)))
            {
                try
                {
                    result = long.Parse(value.ToString());
                }
                catch { }
            }
            return result;
        }
        /// <summary>
        /// 转换长整形 失败返回0
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static long ToSafeLong(object value)
        {
            return ToSafeLong(value, 0);
        }
        
       
    }
}
